package com.springbootdf.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springbootdf.Dto.ItemDto;
import com.springbootdf.common.Result;
import com.springbootdf.entity.Game;
import com.springbootdf.entity.Item;
import com.springbootdf.entity.ItemGame;
import com.springbootdf.entity.User;
import com.springbootdf.service.IItemGameService;
import com.springbootdf.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wy
 * @since 2022-09-24
 */
@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @Autowired
    private IItemGameService itemGameService;

    @GetMapping("/{id}")
    public Result getItemList(@PathVariable Integer id){
        return Result.success(itemService.getItemsById(id));
    }

    @GetMapping
    public IPage<Item> findPages(@RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "5") Integer pageSize
                                ){
        IPage<Item> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper<>();
        return itemService.page(page,queryWrapper);
    }

    @PostMapping("/import")
    public Result imp(MultipartFile file) throws Exception {
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        // 方式1：(推荐) 通过 javabean的方式读取Excel内的对象，但是要求表头必须是英文，跟javabean的属性要对应起来
//        List<User> list = reader.readAll(User.class);

        // 方式2：忽略表头的中文，直接读取表的内容
        List<List<Object>> list = reader.read(1);
        List<Item> items = CollUtil.newArrayList();
        List<ItemGame> itemGames = CollUtil.newArrayList();
        for (List<Object> row : list) {
            Item item = new Item();
            ItemGame itemGame = new ItemGame();
            itemGame.setItemId(Integer.parseInt(row.get(0).toString()));
            itemGame.setGameId(Integer.parseInt(row.get(1).toString()));
            item.setId(Integer.parseInt(row.get(0).toString()));
            item.setUserId(Integer.parseInt(row.get(2).toString()));
            item.setTitle(row.get(3).toString());
            item.setMessage(row.get(4).toString());
            item.setStatus(Integer.parseInt(row.get(5).toString()));
            items.add(item);
            itemGames.add(itemGame);
        }

//        userService.saveBatch(users);
        itemService.saveBatch(items);
        itemGameService.saveBatch(itemGames);
        return Result.success(true);
    }
}

